package com.wpzf.zxhc.controller;

import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.common.security.utils.SecurityUtils;
import com.wpzf.system.api.model.LoginUser;
import com.wpzf.zxhc.utils.ExcelUtils;
import com.wpzf.zxhc.service.XjtbTbStaticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xjtbtbstatic")
public class XjtbTbStaticController {


    @Autowired
    XjtbTbStaticService xjtbTbStaicService;


    @Autowired
    TokenService tokenService;

    @Value("${tempath}")
    public String tempath;

    //县级填报统计分析
    @RequestMapping(value = "/getXjTbByStaticList", method = RequestMethod.POST)
    public AjaxResult getXjTbByStaticList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbByStaticList(parm);
            Integer cnt = xjtbTbStaicService.getXjTbByStaticCount(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list, cnt);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }


    //获取批次
    @RequestMapping(value = "/getXjTbPchList", method = RequestMethod.POST)
    public AjaxResult getXjTbPchList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbPchList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }


    //获取省份
    @RequestMapping(value = "/getXjTbProvList", method = RequestMethod.POST)
    public AjaxResult getXjTbProvList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbProvList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取市区
    @RequestMapping(value = "/getXjTbCityList", method = RequestMethod.POST)
    public AjaxResult getXjTbCityList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbCityList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取区县
    @RequestMapping(value = "/getXjTbXzqList", method = RequestMethod.POST)
    public AjaxResult getXjTbXzqList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbXzqList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }
    //统计各区县不通过的地块图斑数量
    @RequestMapping(value = "/getXjTbByNoPassHcjgList", method = RequestMethod.POST)
    public AjaxResult getXjTbByNoPassHcjgList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbByNoPassHcjgList(parm);
            Integer cnt = xjtbTbStaicService.getXjTbByNoPassHcjgCount(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list, cnt);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取当前省所有不通过的图斑
    @RequestMapping(value = "/export", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public AjaxResult export(@RequestBody Map<String, Object> parm, HttpServletResponse response) {
        ExcelUtils excel = new ExcelUtils();
        String msg = "";
        String rest = "";
        List<String> Colomnlist = new ArrayList<>();
        try {
            parm.put("pageNum",1);
            parm.put("pageSize",1000000);
            String role=parm.get("role")+"";
            Colomnlist.clear();
            Colomnlist.add("序号");
            Colomnlist.add("所属省份");
            Colomnlist.add("所属市区");
            Colomnlist.add("所属区县");
            Colomnlist.add("图斑编号");
            Colomnlist.add("判定类型");
            Colomnlist.add("市级核查结论");
            Colomnlist.add("市级审核说明");
            Colomnlist.add("省级核查结论");
            Colomnlist.add("省级审核说明");
            Colomnlist.add("省级核查结果说明");
            List<Map<String, Object>> list = xjtbTbStaicService.getXjTbByNoPassTbList(parm);
            rest = excel.export(response,"不通过图斑列表", list, Colomnlist, tempath);

            if (rest.contains("error")) {
                msg = "导出失败";
            } else {
                msg = "导出成功";
            }
            return AjaxResult.success(msg, rest);
        } catch (Exception e) {
            e.printStackTrace();
            msg = "导出失败";
        }
        return AjaxResult.error(msg, -1);
    }

    //统计各区县不通过的地块图斑数量
    @RequestMapping(value = "/getXjTbByNoPassTbList", method = RequestMethod.POST)
    public AjaxResult getXjTbByNoPassTbList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            parm.put("xzq", loginUser.getSysUser().getXzqdm());
            String xzqdmjq = loginUser.getSysUser().getXzqdmjq();
            String[] xzqdmjqArray = null;
            if (xzqdmjq != null && !"".equals(xzqdmjq)) {
                xzqdmjqArray = xzqdmjq.split(",");
            }
            parm.put("xzqdmjqArray", xzqdmjqArray);
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbByNoPassTbList(parm);
            Integer cnt = xjtbTbStaicService.getXjTbByNoPassTbCount(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list, cnt);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    @RequestMapping(value = "/getXjTbByNoPassTbListindex", method = RequestMethod.POST)
    public AjaxResult getXjTbByNoPassTbListindex(@RequestBody Map<String, Object> parm) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            parm.put("xzq", loginUser.getSysUser().getXzqdm());
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbByNoPassTbListindex(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取批次
    @RequestMapping(value = "/getXjTbNoPassPchList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassPchList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            parm.put("xzqdm", loginUser.getSysUser().getXzqdm());
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassPchList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }


    //获取省份
    @RequestMapping(value = "/getXjTbNoPassProvList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassProvList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassProvList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取市区
    @RequestMapping(value = "/getXjTbNoPassCityList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassCityList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            LoginUser loginUser = SecurityUtils.getLoginUser();
            String xzqdmjq = loginUser.getSysUser().getXzqdmjq();
            String[] xzqdmjqArray = null;
            if (xzqdmjq != null && !"".equals(xzqdmjq)) {
                xzqdmjqArray = xzqdmjq.split(",");
            }
            parm.put("xzqdmjqArray", xzqdmjqArray);
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassCityList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取区县
    @RequestMapping(value = "/getXjTbNoPassXzqList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassXzqList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassXzqList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取批次
    @RequestMapping(value = "/getXjTbNoPassSPchList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassSPchList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassSPchList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }


    //获取省份
    @RequestMapping(value = "/getXjTbNoPassSProvList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassSProvList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassSProvList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取市区
    @RequestMapping(value = "/getXjTbNoPassSCityList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassSCityList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassSCityList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }

    //获取区县
    @RequestMapping(value = "/getXjTbNoPassSXzqList", method = RequestMethod.POST)
    public AjaxResult getXjTbNoPassSXzqList(@RequestBody Map<String, Object> parm) {
        try {
            //获取图斑列表
            List<Map<String,Object>> list = xjtbTbStaicService.getXjTbNoPassSXzqList(parm);
            if (list != null) {
                return AjaxResult.success("获取成功", list);
            } else {
                return AjaxResult.error("获取失败");
            }
        } catch (Exception ex) {
            return AjaxResult.error("获取失败", ex.toString());
        }
    }
}
